package com.example.controller;

import com.example.service.BlockingQueueComparisonService;
import com.example.service.BlockingQueueExampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/blocking/queue/example")
public class BlockingQueueExampleController {

    @Autowired
    private BlockingQueueExampleService blockingQueueExampleService;
    @Autowired
    private BlockingQueueComparisonService blockingQueueComparisonService;

    @GetMapping("/produce")
    public String produce(@RequestParam String message) throws InterruptedException {
        blockingQueueExampleService.produce(message);
        return "消息已生产: " + message;
    }

    @GetMapping("/consume")
    public String consume() throws InterruptedException {
        blockingQueueExampleService.consume();
        return "消息已消费";
    }

    @GetMapping("/queue/remaining-capacity")
    public String getRemainingCapacity() {
        int capacity = blockingQueueExampleService.getRemainingCapacity();
        return "Remaining capacity of the queue: " + capacity;
    }

    @GetMapping("/queue/offer")
    public String offerMessage(@RequestParam String message) {
        blockingQueueComparisonService.tryOffer(message);
        return "Attempted to offer message: " + message;
    }

    @GetMapping("/queue/put")
    public String putMessage(@RequestParam String message) throws InterruptedException {
        blockingQueueComparisonService.tryPut(message);
        return "Successfully put message: " + message;
    }

    @GetMapping("/queue/poll")
    public String pollMessage() {
        blockingQueueComparisonService.tryPoll();
        return "Attempted to poll message";
    }

    @GetMapping("/queue/take")
    public String takeMessage() throws InterruptedException {
        blockingQueueComparisonService.tryTake();
        return "Successfully took message";
    }

    @GetMapping("/queue/offer-with-timeout")
    public String offerMessageWithTimeout(@RequestParam String message) throws InterruptedException {
        blockingQueueComparisonService.tryOfferWithTimeout(message);
        return "Attempted to offer message with timeout: " + message;
    }

    @GetMapping("/queue/poll-with-timeout")
    public String pollMessageWithTimeout() throws InterruptedException {
        blockingQueueComparisonService.tryPollWithTimeout();
        return "Attempted to poll message with timeout";
    }


}
